package cz.hanis.android.geography.activities;


import java.util.Comparator;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.SearchView;

import cz.hanis.android.geography.CountryTools;
import cz.hanis.android.geography.GeographyApplication;
import cz.hanis.android.geography.R;
import cz.hanis.android.geography.adapters.CountryArrayAdapter;
import cz.hanis.android.geography.comparator.ByAlphabetComparator;
import cz.hanis.android.geography.comparator.ByAreaComparator;
import cz.hanis.android.geography.comparator.ByPopulationComparator;
import cz.hanis.android.geography.models.Continent;
import cz.hanis.android.geography.models.Country;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class CountryListActivity extends SherlockActivity implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {

	private CountryTools mCountryTool;
	private SearchView mSearchView;
	private CountryArrayAdapter mAdapter;
	
	private ByAlphabetComparator mByAlphabetComparator = new ByAlphabetComparator();	
	private ByAreaComparator mByAreaComparator = new ByAreaComparator();
	private ByPopulationComparator mByPopulationComparator = new ByPopulationComparator();
	
	private Comparator<Country> mSelectedComparator;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.country_list);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		 
		mCountryTool = GeographyApplication.getInstance().getCountryTool();
		mAdapter = new CountryArrayAdapter(this, mCountryTool.getCountries(Continent.ALL));		
		ListView countryList = (ListView) findViewById(R.id.lv_country_list);
		countryList.setAdapter(mAdapter);	
		mSelectedComparator = mByAlphabetComparator;
		sortCountries();
		countryList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				onClick(position);
			}
		});		
	}

	
	private void onClick(int position) {
		Intent intent = new Intent(CountryListActivity.this,
				CountryDetailActivity.class);
		mCountryTool.setTmpCountryForList(mAdapter.getCountries());
		intent.putExtra(CountryDetailActivity.COUNTRY_KEY, position);
		startActivity(intent);
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.country_list_menu, menu);;
        
        mSearchView = (SearchView) menu.findItem(R.id.cl_menu_search).getActionView();
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setOnCloseListener(this);
        return super.onCreateOptionsMenu(menu);
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent parentActivityIntent = new Intent(this, WelcomeActivity.class);
                parentActivityIntent.addFlags(
                        Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(parentActivityIntent);
                finish();
                return true;
            case R.id.cl_submenu_sort_by_alphabet:
            	mSelectedComparator = mByAlphabetComparator;
            	sortCountries();
            	return true;
            case R.id.cl_submenu_sort_by_area:
            	mSelectedComparator = mByAreaComparator;
            	sortCountries();            	
            	return true;
            case R.id.cl_submenu_sort_by_population:
            	mSelectedComparator = mByPopulationComparator;
            	sortCountries();
            	return true;    
            case R.id.cl_submenu_filter_all:
            	filterBy(Continent.ALL);
            	return true;     
            case R.id.cl_submenu_filter_europe:
            	filterBy(Continent.EUROPE);
            	return true;   
            case R.id.cl_submenu_filter_asia:
            	filterBy(Continent.ASIA);
            	return true;   
            case R.id.cl_submenu_filter_africa:
            	filterBy(Continent.AFRICA);
            	return true;   
            case R.id.cl_submenu_filter_north_america:
            	filterBy(Continent.NORTH_AMERICA);
            	return true;   
            case R.id.cl_submenu_filter_south_america:
            	filterBy(Continent.SOUTH_AMERICA);
            	return true;   
            case R.id.cl_submenu_filter_oceania:
            	filterBy(Continent.OCEANIA);
            	return true;   
//            case R.id.cl_submenu_filter_antarctica:
//            	filterBy(Continent.ANTARCTICA);
//            	return true;               	
            default:
                return super.onOptionsItemSelected(item);
        }
    }    
    
    private void sortCountries() {
    	mAdapter.sort(mSelectedComparator);
    }
    
    private void filterBy(String continent) {
    	mAdapter.setCountries(mCountryTool.getCountries(continent));
    	sortCountries();    	
    }
    

    public boolean onQueryTextChange(String query) {        
		if (query.length() > 0) {
			mAdapter.setCountries(mCountryTool.getCountriesWithPrefix(query));
		} else {
			mAdapter.setCountries(mCountryTool.getCountries(Continent.ALL));
		}
        return false;
    }
 
    public boolean onQueryTextSubmit(String query) {
    	return onQueryTextChange(query);
    }
    
    public boolean onClose() {
    	mAdapter.setCountries(mCountryTool.getCountries(Continent.ALL));   	
        return false;
    }


}
